<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Doctor extends Controller_DefaultTemplate {

    function action_index() {
        $username = "";
        $password = "";
        $error = "";

        $username = session::instance()->get('username');

        if(isset($username)) {
            $this->request->redirect("../doctor/listPatient/");
        }

        if(isset($_POST['username'])) {
            $username = $_POST['username'];
            $password = $_POST['password'];

            $doctorModel = new Model_Doctor();
            $result = $doctorModel->login($username, $password);
            
            if($result){
                $this->request->redirect("../doctor/listPatient/");
            }else{
                $error = "Invalid Username of password";
            }
        }

        $this->template->title            = 'TeleStroke';
        $this->template->meta_keywords    = 'TeleStroke';
        $this->template->meta_description = 'TeleStroke';
        $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
        $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
        $this->template->content = View::factory("doctor/login")
                ->set('username',$username)
                ->set('error',$error);
    }

    function action_listPatient() {
        $username = session::instance()->get('username');
        if(!isset($username)) {
            $this->request->redirect("../doctor/");
        }

        $consult_doctor_id = session::instance()->get('consult_doctor_id');
        $consult = new Model_Consult();

        if(isset($username)) {
            if(session::instance()->get('role')!="doctor") {
                echo "You don't have permission to access this page";
            }else {
                $consulting = $consult->getConsultingPatient($consult_doctor_id);
                if(count($consulting)!=0) {

                    $this->request->redirect("../doctor/consultpage?p_id=".$consulting[0]['p_id']);
                }

                $patients = null;
                $patients = $consult->getWaitingPatient($consult_doctor_id);
                $histories = $consult->getConsultHistoryByConsultDoctorId($consult_doctor_id);
                $this->template->title            = 'TeleStroke';
                $this->template->meta_keywords    = 'TeleStroke';
                $this->template->meta_description = 'TeleStroke';
                $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
                $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min','assets/js/jquery.timer.js');
                $this->template->content = View::factory("doctor/listpatient")
                        ->set('consult_doctor_id',$consult_doctor_id)
                        ->set('patients',$patients)
                        ->set('histories',$histories);
            }
        }else {
            $this->request->redirect("../doctor/");
        }
    }

    function action_changepassword(){
        $message = "";
        $username = session::instance()->get('username');
        $consult_doctor_id = session::instance()->get('consult_doctor_id');
        if(!isset($username)) {
            $this->request->redirect("../");
        }else {
            if(session::instance()->get('role')!="doctor") {
                echo "You don't have permission to access this page";
            }else {
                $doctorModel = new Model_Doctor();
                if(isset($_POST['method'])) {
                    $method = $_POST['method'];
                    if($method=='changepassword') {
                        $result = $doctorModel->changepassword($consult_doctor_id,$_POST['password'],$_POST['new_password']);
                        if($result=="0"){
                            $message = "เปลี่ยน password สำเร็จ login เข้าสู่ระบบอีกครั้งด้วย password ใหม่";
                            echo "<script language=javascript>alert(\"$message\")</script>";
                            $this->request->redirect("../doctor/logout");
                        }
                        else if($result=="1"){
                            
                            $message = "old passwold ไม่ถูกต้อง";
                        }
                    }
                }
               
                $this->template->title            = 'TeleStroke';
                $this->template->meta_keywords    = 'TeleStroke';
                $this->template->meta_description = 'TeleStroke';
                $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
                $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
                $this->template->content = View::factory("doctor/changepassword")
                        ->set('message',$message)
                        ->set('consult_doctor_id',$consult_doctor_id);
            }
        }
    }

    function action_edit(){
        $message = "";
        $username = session::instance()->get('username');
        $consult_doctor_id = session::instance()->get('consult_doctor_id');
        if(!isset($username)) {
            $this->request->redirect("../");
        }else {
            if(session::instance()->get('role')!="doctor") {
                echo "You don't have permission to access this page";
            }else {
                $doctorModel = new Model_Doctor();
                if(isset($_POST['method'])) {
                    $method = $_POST['method'];
                    if($method=='edit') {
                        $doctorModel->updateProfile($_POST);
                        $message = "แก้ไขข้อมูลเรียบร้อย";
                    }
                }

                $doctor = $doctorModel->getDoctorProfileFromConsult_doctor_id($consult_doctor_id);

                $this->template->title            = 'TeleStroke';
                $this->template->meta_keywords    = 'TeleStroke';
                $this->template->meta_description = 'TeleStroke';
                $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
                $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
                $this->template->content = View::factory("doctor/doctor_edit")
                        ->set('message',$message)
                        ->set('consult_doctor_id',$consult_doctor_id)
                        ->set('doctor',$doctor[0]);
            }
        }
    }

    function action_logout() {
        $doctor_model = new Model_Doctor();
        $doctor_model->logout();
        $this->request->redirect("../doctor/");
    }

    function action_consultPage(){
        $username = session::instance()->get('username');
        if(!isset($username)) {
             $this->request->redirect("../doctor/");
        }

        if(isset($_GET['p_id'])) {
            $patient_model = new Model_Patient();
            $consult = new Model_Consult();
            $p_id =  $_GET['p_id'];
            $patient= $patient_model->getPatientProfileFromPID($p_id);
            $c_id = $consult->getConsultID($p_id);

            $status = $consult->checkStatus($p_id, $c_id);
            if($status=="Waiting to consult")
                 $consult->updateToConsultingStatus($p_id);
            elseif($status=="Consulting")
                 $consult->updateToConsultingStatus($p_id);
            elseif($status==null)
                $this->request->redirect("../doctor/listPatient");

        }else {
            $this->request->redirect("../doctor/");
        }
        $this->template->title            = 'TeleStroke';
        $this->template->meta_keywords    = 'TeleStroke';
        $this->template->meta_description = 'TeleStroke';
        $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
        $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
        $this->template->content = View::factory("doctor/consultpage")
                ->set('patient',$patient[0])
                ->set('p_id',$p_id);
    }
       
    function action_consult() {
        $username = session::instance()->get('username');
        if(!isset($username)) {
          $username = session::instance()->get('username');   $this->request->redirect("../doctor/");
        }

        $patient_model = new Model_Patient();
        $consult = new Model_Consult();
        
        $std = null;
        $nihss = null;
        $stp = null;
        $est = null;
        $lab = null;
        $mng = null;

        if(isset($_POST['p_id'])) {
            $p_id = $_POST['p_id'];
            $c_id = $consult->getConsultID($p_id);
            $patient= $patient_model->getPatientProfileFromPID($p_id);
        }elseif(isset($_GET['p_id'])) {
            $p_id = $_GET['p_id'];
            $c_id = $consult->getConsultID($p_id);
            $patient= $patient_model->getPatientProfileFromPID($p_id);
        }else {
            $this->request->redirect("../doctor/listPatient");
        }

       
        //Load current data
        $std = $consult->loadStrokeData($p_id, $c_id);
        $nihss = $consult->loadNIHSSData($p_id, $c_id);
        $lab = $consult->loadLaboratoryData($p_id, $c_id);
        $stp = $consult->loadStrokePhy($p_id, $c_id);
        $est = $consult->loadEstimateData($p_id, $c_id);
        $mng = $consult->loadManagementData($p_id, $c_id);

        $this->template->title            = 'TeleStroke';
        $this->template->meta_keywords    = 'TeleStroke';
        $this->template->meta_description = 'TeleStroke';
        $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
        $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min','assets/js/consultdoctor.js','assets/js/jquery.timer.js');
        $consult_view = View::factory("doctor/doctor_consult")
                ->set('patient',$patient[0])
                ->set('std',$std)
                ->set('stp',$stp)
                ->set('p_id',$p_id)
                ->set('c_id', $c_id)
                ->set('lab',$lab)
                ->set('est',$est)
                ->set('mng',$mng)
                ->set('nihss', $nihss);
        $this->template->content = $consult_view;
    }

    function action_ipcamera(){
        $username = session::instance()->get('username');

        if(!isset($username)) {
             $this->request->redirect("../doctor/");
        }

        if(isset($_GET['p_id'])) {
            $consult = new Model_Consult();
            $p_id = $_GET['p_id'];
            $c_id = $consult->getConsultID($p_id);
            $doctor = $consult->getDoctorData($p_id,$c_id);
            $ipcamera = $consult->getIPCameraData($p_id, $c_id);
        }

        $this->template->title            = 'TeleStroke';
        $this->template->meta_keywords    = 'TeleStroke';
        $this->template->meta_description = 'TeleStroke';
        $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
        $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min','assets/js/jquery.timer.js');
        $this->template->content = View::factory("camera/doctor")
                ->set('url',$ipcamera['pp_url'])
                ->set('doctor',$doctor)
                ->set('username',$ipcamera['pp_username'])
                ->set('password',$ipcamera['pp_password'])
                ->set('p_id',$p_id)
                ->set('c_id', $c_id)
            ;
    }

    function action_live(){
        $this->template->title            = 'TeleStroke';
        $this->template->meta_keywords    = 'TeleStroke';
        $this->template->meta_description = 'TeleStroke';
        $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
        $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min','assets/js/jquery.timer.js');
        $this->template->content = View::factory("camera/liveDoctor");
    }
    
    function action_editct(){
        $username = session::instance()->get('username');
        if(!isset($username)) {
             $this->request->redirect("../doctor/");
        }

        $img_path = $_GET['path'];
        $c_id = $_GET['c_id'];
        $p_id = $_GET['p_id'];
        $ct_id = $_GET['ct_id'];
        $img_width = $_GET['img_width'];
        $img_height = $_GET['img_height'];
        $comment_id = $_GET['comment_id'];
        $this->template->title            = 'TeleStroke';
        $this->template->meta_keywords    = 'TeleStroke';
        $this->template->meta_description = 'TeleStroke';
        $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
        $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min','assets/js/editct.js');
        $this->template->content = View::factory("doctor/editct")
                ->set('img_path',$img_path)
                ->set('c_id',$c_id)
                ->set('p_id',$p_id)
                ->set('ct_id',$ct_id)
                ->set('img_width',$img_width)
                ->set('img_height',$img_height)
                ->set('comment_id',$comment_id)
            ;
    }
}
?>
